﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MtApi;
using MtApi.Monitors;

namespace MTEA
{
    class MTStatusDisplay
    {
        private MtApiClient client;
        public static readonly string Tag = "MTStatusDisplay";
        public MTStatusDisplay(MtApiClient client)
        {
            this.client = client;
            client.ConnectionStateChanged += DisplayConnectionStateChanged;
            //client.QuoteUpdated += DisplayQuoteUpdated;
            TimerTradeMonitor tradeMonitor=new TimerTradeMonitor(client) {Interval = 1000};
            tradeMonitor.AvailabilityOrdersChanged += DisplayAvailabilityOrdersChanged;
            tradeMonitor.Start();
        }

        public void DisplayOrders()
        {
            var orders=client.GetOrders(OrderSelectSource.MODE_TRADES).ToArray();
            foreach (var mtOrder in orders)
            {
                Console.WriteLine(@"Order Id={0} Type={1} OpenPrice={2} ClosePrice={3} Lots={4}",mtOrder.Ticket,mtOrder.Operation.ToString(),mtOrder.OpenPrice,mtOrder.ClosePrice,mtOrder.Lots);
            }
        }


        public void DisplayHistoryOrders()
        {
            var orders = client.GetOrders(OrderSelectSource.MODE_HISTORY).ToArray();
            foreach (var mtOrder in orders)
            {
                Console.WriteLine(@"Order Id={0} Type={1} OpenPrice={2} ClosePrice={3} Lots={4}", mtOrder.Ticket, mtOrder.Operation.ToString(), mtOrder.OpenPrice, mtOrder.ClosePrice, mtOrder.Lots);
            }
        }

        public void DisplayAvailabilityOrdersChanged(object sender, AvailabilityOrdersEventArgs e)
        {
            if (e.Opened != null && e.Opened.Count != 0)
            {
                Console.WriteLine($"{sender.GetType()}: Opened orders - {string.Join(", ", e.Opened.Select(o => o.Ticket).ToList())}");
            }

            if (e.Closed != null && e.Closed.Count!=0)
            {
                Console.WriteLine($"{sender.GetType()}: Closed orders - {string.Join(", ", e.Closed.Select(o => o.Ticket).ToList())}");
            }
        }

        private void DisplayConnectionStateChanged(object sender, MtConnectionEventArgs e)
        {
            MTLogcat.i(Tag,e.ConnectionMessage);
            //Console.WriteLine(e.ConnectionMessage);
        }

        private void DisplayQuoteUpdated(object sender, string symbol, double bid, double ask)
        {
            Console.WriteLine(@"Quote: Symbol = {0}, Bid = {1}, Ask = {2}", symbol, bid, ask);
            MtApiClient client = sender as MtApiClient;
            double ma5 = client.iMA(symbol, 0, 5, 0, 0, 0, 0);
            double ma10 = client.iMA(symbol, 0, 10, 0, 0, 0, 0);
            Console.WriteLine(@"       TimeFrame=M1 MA5 = {0}, MA10 = {1}", ma5, ma10);
            ma5 = client.iMA(symbol, (int)ENUM_TIMEFRAMES.PERIOD_H1, 5, 0, 0, 0, 1);
            ma10 = client.iMA(symbol, (int)ENUM_TIMEFRAMES.PERIOD_H1, 10, 0, 0, 0, 1);
            Console.WriteLine(@"       TimeFrame=H1 MA5 = {0}, MA10 = {1}", ma5, ma10);
            ma5 = client.iMA(symbol, (int)ENUM_TIMEFRAMES.PERIOD_D1, 5, 0, 0, 0, 1);
            ma10 = client.iMA(symbol, (int)ENUM_TIMEFRAMES.PERIOD_D1, 10, 0, 0, 0, 1);
            Console.WriteLine(@"       TimeFrame=D1 MA5 = {0}, MA10 = {1}", ma5, ma10);


            Console.WriteLine(@"");
        }
    }
}
